package com.yc.jpyy.mobile.controller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/questionlist")
public class GetQuestionListInfo extends BaseController{
	@Autowired
	private BizService bizService;
	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(GetQuestionListInfo.class);
	
	/**
	 * 题目信息查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetquestionListInfo", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetquestionListInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		try {
			
			String PageNum = request.getParameter("PageNum");
			String PageSize = request.getParameter("PageSize");

			if (PageNum != null && !PageNum.isEmpty() || PageSize != null
					&& !PageSize.isEmpty()) {
				if (PageNum.equals("0") || PageSize.equals("0")) {
					response.setErrorcode(1);
					response.setMessage("页码或页数不能为0...");
					return response.toJSON();
				}
			}

			// 练习类型
			String studyType = request.getParameter("studyType");
			// 章节类型
			String parmType = request.getParameter("parmType");
			// 科目
			String subjectId = request.getParameter("subjectId");
			// 学驾类型
			String driveType = request.getParameter("driveType");
			// 用户Id
			String userId = request.getParameter("userId");
			if(studyType != null && !studyType.isEmpty() 
					 && driveType != null && !driveType.isEmpty()
					 && userId != null && !userId.isEmpty()){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("imgUrl", Config.getImageStudyAddress());
				valMap.put("PageNum", PageNum);
				valMap.put("PageSize", PageSize);
				valMap.put("parmType", parmType);
				valMap.put("subjectId", subjectId);
				valMap.put("driveType", driveType);
				valMap.put("studyType", studyType);
				valMap.put("userId", userId);
				//查询试题
				response = bizService.selectQuestion(valMap,response);		
			}else{
				response.setErrorcode(1);
				response.setMessage("练习类型、学驾类型不能为空...");
			}

		} catch (Exception e) {
			System.out.println("GetquestionListInfo:"+ e.toString());
			response.setReturnMsg(1,"获取试题信息失败！");
		} finally {
			return response.toJSON();
		}

	}
	
	/**
	 * 获取章节分类
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSecondConfigInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSecondConfigInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			String trainTypeName = request.getParameter("TrainTypeName"); 
			if(subjectId != null && !subjectId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("trainTypeName", trainTypeName);
				//获取章节分类列表
				response = bizService.getSecondConfigInfo(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("科目类型不能为空...");
			}

		} catch (Exception e) {
			logger.info("getSecondConfigInfo:"+ e.toString());
			response.setReturnMsg(1,"获取分类信息失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 获取收藏试题
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCollectionQuestions", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCollectionQuestions(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//用户ID
			String userId = request.getParameter("userId"); 
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			// 学驾类型
			String driverType = request.getParameter("driverType");
			
			if(userId != null && !userId.isEmpty()
					&& subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("userId", userId);
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				//获取收藏试题列表
				response = bizService.getCollectionQuestions(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("getCollectionQuestions:"+ e.toString());
			response.setReturnMsg(1,"获取分类信息失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 获取错题试题
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getWrongQuestions", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getWrongQuestions(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//用户ID
			String userId = request.getParameter("userId"); 
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			// 学驾类型
			String driverType = request.getParameter("driverType");
			
			if(userId != null && !userId.isEmpty()
					&& subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("userId", userId);
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				//获取错题试题列表
				response = bizService.getWrongQuestions(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("getWrongQuestions:"+ e.toString());
			response.setReturnMsg(1,"获取错误试题失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 删除错题记录
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/deleteWrongQuestions", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String deleteWrongQuestions(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//用户ID
			String userId = request.getParameter("userId"); 
			//试题Id
			String questionId = request.getParameter("questionId");
			if(userId != null && !userId.isEmpty()
					&& questionId != null && !questionId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("userId", userId);
				paramsMap.put("questionId", questionId);
				//获取错题试题列表
				response = bizService.deleteWrongQuestions(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("deleteWrongQuestions:"+ e.toString());
			response.setReturnMsg(1,"删除错题失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 获取模拟试题
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSimulateQuestions", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSimulateQuestions(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			// 学驾类型
			String driverType = request.getParameter("driverType");
			
			if(subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				//获取错题试题列表
				response = bizService.getSimulateQuestions(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("getSimulateQuestions:"+ e.toString());
			response.setReturnMsg(1,"获取模考试题失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 获取模考信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSimulateInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSimulateInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			//学员ID
			String studentId = request.getParameter("studentId"); 
			
			Map<String,Object> paramMap = new HashMap<>();
			if(subjectId != null && !subjectId.isEmpty()){
				paramMap.put("subjectId", subjectId);
				paramMap.put("studentId", studentId);
				if(subjectId.equals("1")){
					paramMap.put("subject", "1");
					paramMap.put("subjectId", "12");
				}else if(subjectId.equals("4")){
					paramMap.put("subject", "4");
					paramMap.put("subjectId", "42");
				}
				//获取错题试题列表
				response = bizService.getSimulateInfo(paramMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("getSimulateInfo:"+ e.toString());
			response.setReturnMsg(1,"获取分类信息失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 获取难题试题
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getDifficultQuestions", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getDifficultQuestions(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			// 学驾类型
			String driverType = request.getParameter("driverType");
			//用户Id
			String userId = request.getParameter("userId");
			
			if(subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()
					&& userId != null && !userId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				paramsMap.put("userId", userId);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				//获取错题试题列表
				response = bizService.getDifficultQuestions(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("getDifficultQuestions:"+ e.toString());
			response.setReturnMsg(1,"获取模考试题失败！");
		} finally {
			return response.toJSON();
		}
	}
}
